﻿using Jinjia.Approve.IRepository;
using Jinjia.Approve.IService;
using Jinjia.Approve.Service.Base;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.IService.JJFlow;
using Jinjia.Approve.Model;
using Jinjia.Approve.Model.Input;
using Jinjia.Approve.Model.VM.JJFlow;
using System;

namespace Jinjia.Approve.Service
{
    public class JJFlowCommentService : BaseService, IJJFlowCommentService
    {
        private readonly IMapper _mapper;
        private readonly IJJFlowNodeService _jjFlowNodeService;
        private readonly IJJFlowInstanceService _jjFlowInstanceService;
        private readonly IJJFlowCommentRepository _jjFlowCommentRepository;

        public JJFlowCommentService(IMapper mapper,
            IJJFlowNodeService jjFlowNodeService,
            IJJFlowInstanceService jJFlowInstanceService,
            IJJFlowCommentRepository jJFlowCommentRepository)
        {
            _mapper = mapper;
            _jjFlowNodeService = jjFlowNodeService;
            _jjFlowInstanceService = jJFlowInstanceService;
            _jjFlowCommentRepository = jJFlowCommentRepository;
        }

        public async Task<int> Save(JJFlowCommentVM data)
        {
            var jjFlowCommentTbl = _mapper.Map<jj_flow_comment_tbl>(data);
            return await _jjFlowCommentRepository.Save(jjFlowCommentTbl);
        }

        public async Task UpdateIsAppend(string biz_order_id, string modular_key)
        {
            //取出所有评论
            var comments = await _jjFlowCommentRepository.GetCommentByBizOrderIdAndModularKey(biz_order_id, modular_key);
            //取出最新的一条数据
            var latest_comment = comments.Where(x => x.deleted == 0 && string.IsNullOrWhiteSpace(x.comment) == false)
                .OrderByDescending(x => x.id).FirstOrDefault();
            var ids = comments.Select(x => x.id).ToList();
            //先将所有的数据is_append变更为0
            await _jjFlowCommentRepository.UpdateIsAppend(ids, 0);
            if (latest_comment != null)
            {
                //将最新一条有内容的评论is_append变更为1
                await _jjFlowCommentRepository.UpdateIsAppend(latest_comment.id, 1);
            }
        }

        public Task<List<JJFlowCommentVM>> GetJJFlowComments(JJFlowCommentSearchInput input)
        {
            return _jjFlowCommentRepository.GetJJFlowCommentsWhitFilter(input);
        }

        public Task<int> SaveComment(JJFlowCommentCreateInput input)
        {
            return _jjFlowCommentRepository.InsertCommentAndFileInfo(input);
        }

        public async Task SaveComment(jj_flow_track jjFlowTrack)
        {
            var instance = await _jjFlowInstanceService.GetById(jjFlowTrack.instance_id);
            if (instance != null)
            {
                var now = DateTime.Now;
                //保存评论
                await _jjFlowCommentRepository.Save(new jj_flow_comment_tbl()
                {
                    comment = jjFlowTrack.remark,
                    node_name = jjFlowTrack.node_name,
                    node_action = jjFlowTrack.action_type_name,
                    is_append = 0,
                    biz_order_id = instance.BizOrderId,
                    modular_key = instance.ModularKey,
                    create_time = now,
                    update_time = now,
                    update_user_id = jjFlowTrack.update_user_id,
                    create_user_id = jjFlowTrack.create_user_id
                });

                //轮询更新之前评论的状态
                await _jjFlowCommentRepository.UpdateIsAppend(instance.BizOrderId, instance.ModularKey);
            }
        }
    }
}